import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'react';

import {
    Button,
    DatePicker,
    Form,
    Input,
    Modal,
    Radio,
    Tag,
    Select,
    InputNumber,
    message,
    Row,
    Col,
    Checkbox,
    Switch,
} from 'antd';
import { modalLayoutLg, modalLayoutMd } from '@/config/layout';
import { request } from 'umi';
const DictModal = (props: any, refs: any) => {
    const [form] = Form.useForm();
    // const [fileList,setfileList] = useState([]);
    const [measureClassData, setmeasureClassData] = useState([]);
    const { setFieldsValue, resetFields } = form;


    const [code, setCode] = useState('');
    const [isModalVisible, setIsModalVisible] = useState(false);
    const [userList, setUserList] = useState([]);
    //显示弹窗
    const showModal = () => {
        setIsModalVisible(true);
    };
    //页面加载初始化
    useEffect(() => {
        getUserList();

    }, []);
    //获取用户集合
    const getUserList = () => {
        request('/sys/dictUtils/findUserList', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            data: {},
        }).then((res) => {
            let list = res?.data || [];
            list.map((e, i) => {
                list[i]['label'] = e['name'];
                list[i]['value'] = e['id'];
            });
            setUserList(list);
        });
        //
    };
    //取消
    const handleCancel = () => {
        setIsModalVisible(false);
        setCode('');
        resetFields();
        props.callback && props.callback();
    };

    //确定
    const handleOk = () => {
        form
            .validateFields()
            .then((vals) => {
                let Obj = form.getFieldsValue();
                Obj = {
                    ...Obj,

                };

                const url = "/sys/accountExtend/saveExtendUser";
                if (code) {
                    Obj.id = code;
                } else {
                }
                request(url, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    data: Obj
                }).then((res) => {
                    message.success(res?.meta?.msg);
                    setIsModalVisible(false);
                    handleCancel();
                })
            })
            .catch((info) => { });
    };


    const getDetail = (id: any) => {
        request('/sys/accountExtend/queryById?id=' + id, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
            data: {
                id,
            },
        }).then((res) => {


            setFieldsValue(res?.data);
            showModal();
        });
    };





    useImperativeHandle(refs, () => ({
        init: (id: any, record: any) => {
            if (id) {
                debugger;
                setCode(id);
                getDetail(id);
            } else {
                resetFields();
                showModal();
            }

            // setfileList([]);
        },
    }));

    return (
        <Modal
            title={code === '' ? '新增' : '编辑'}
            visible={isModalVisible}
            onOk={handleOk}
            onCancel={handleCancel}
            centered
            width={500}
            maskClosable={false}
            bodyStyle={{
                maxHeight: window.innerHeight - 150,
                overflowY: 'auto',
            }}
        >
            <Form form={form} {...modalLayoutMd}>
                <Form.Item
                    label="原用户"
                    name="workExtendUserId"
                    rules={[{ required: true, message: '请选择需要继任的用户' }]}
                >
                    <Select>
                        {userList.map((e: any, i: number) => {
                            return (
                                <Option key={i} value={e.id}>
                                    {e.name}
                                </Option>
                            );
                        })}
                    </Select>
                </Form.Item>

            </Form>
        </Modal>
    );
};
const AddMCom = forwardRef(DictModal);

export default AddMCom;
